New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
set theory v2 #7534
set theory v2 #7534
Conversation
this should solve issues presented in #7495, without a need for a workaround |
Would it be possible to check the types of the arguments instead of the try/except? |
I can check for dicts, but other types can be passed that are not But considering that most of this should be comming from yaml, it should be Brian Coca |
actually, after thinking for 10s: if isinstance(a, collections.Hashable) : ... |
if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable): | ||
c = set(a) | set(b) | ||
else: | ||
c = a + b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice that, conceptually, the set(a) | set(b)
operation is different than a + b
. The former performs set union meaning that duplicates are removed, whereas the latter is a simple concatenation with the possibility for the same item to appear twice.
If these are considered "set operations", then the else part should be replaced by something like:
c = a + filter(lambda x: x not in a, b)
thanks, I had changed the rest, I'm not sure why I missed this one |
Actually @mpdehaan pointed out that, by turning inputs into sets, we are removing duplicates in them too. Hence, the fallback operations are all wrong here. Example with hashable items:
Example with non-hashable items:
Also, the exact same result (than the set operations) is impossible to achieve because sets are implicitly unordered (meaning that you can get varying orders when iterating it) whereas lists are ordered by definition. Anyway, I guess this is an acceptable trade-off... |
I'll resubmit tonight, also making changes to try to coerce always |
This even fixes an issue when using filters in with_items (e.g. #8055) |
This patch fixes #8141 for me. +1 |
+1 as we keep seeing people bitten by the use of non hashable objects with the set theory filters |
Merged, thanks! |
Now it can handle non hashable items like dicts (which used to produce a TypeError)
Also it now makes each list unique before operating on them